package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"hotel/model/mysql"

	"hotel/hotelrpc/hotelrpc"
	"hotel/hotelrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善
func (l *UserUpdateLogic) UserUpdate(in *hotelrpc.UserUpdateRequest) (*hotelrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.Hotel
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在")
	}
	hotel := mysql.Hotel{
		Model: gorm.Model{ID: user.ID},
		Image: in.Image,
		Sex:   in.Sex,
		Sign:  in.Sign,
	}
	mysql.DB.Debug().Updates(&hotel)
	return &hotelrpc.UserUpdateResponse{
		Id: int64(hotel.ID),
	}, nil
}
